package models

import (
	"coll-back/appconfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var err error
var DB *gorm.DB

func Init() {
	conf := appconfig.Config.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		conf.User,
		conf.Password,
		conf.Host,
		conf.Port,
		conf.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库连接成功")
	err = DB.AutoMigrate(
		&User{},
		&Novel{},
		////////15.2////////
		&UserRole{},
		&Role{},
		&RoleNode{},
		&Node{},
		&Depot{},
		&DepotOrder{},
	)
	if err != nil {
		panic(err)
	}
	fmt.Println("数据库迁移成功")
}
